<?php
include './config.inc.php';
include './include/db_mysql.class.php';
$ucdb = new dbstuff;
$ucdb->connect(UC_DBHOST, UC_DBUSER, UC_DBPW, UC_DBNAME, $pconnect);

$db = new dbstuff;
$db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);

function getmaxuid() {
	global $ucdb;
	$query = $ucdb->query("SHOW CREATE TABLE ".UC_DBTABLEPRE."members");
	$data = $ucdb->fetch_array($query);
	//print_r( $data );
	$data = $data['Create Table'];
	if(preg_match('/AUTO_INCREMENT=(\d+?)[\s|$]/i', $data, $a)) {
		return $a[1]-1;
	} else {
		return 0;
	}
}

$maxuid = getmaxuid();

$query = $db->query("SELECT * FROM user_basic_info");

while($data = $db->fetch_array($query)) {
	$data['nick'] = addslashes($data['nick']);
	$uc_prefix = '/var/www/html/ucenter/data/avatar/';
	$avatar = $uc_prefix.get_avatar( $data['id'], "big" );
	copy_avatar(  $uc_prefix, $avatar, $data );
	$avatar = $uc_prefix.get_avatar( $data['id'], "middle" );
	copy_avatar(  $uc_prefix, $avatar, $data );
	$avatar = $uc_prefix.get_avatar( $data['id'], "small" );
	copy_avatar(  $uc_prefix, $avatar, $data );
	//if(file_exists($avatar)) {
		//do nothing
	//}
	//else{
		//mkdir first
	//}	
}

function copy_avatar( $uc_prefix, $avatar, $data ){
	$cmd = "mkdir -p ".get_dir($uc_prefix, $data['id']);
	echo $cmd."\n";
	system( $cmd );
	if( !empty( $data['logo_path'] ) ){
		$logo_path = "/var/www/html/static/upload/avatar/".$data['logo_path'];
		$cmd = "cp $logo_path $avatar";
		echo $cmd."\n";
		system( $cmd );
	}
}

function get_dir($prefix, $uid){
	$uid = abs(intval($uid));
	$uid = sprintf("%09d", $uid);
	$dir1 = substr($uid, 0, 3);
	$dir2 = substr($uid, 3, 2);
	$dir3 = substr($uid, 5, 2);
	return $prefix.$dir1.'/'.$dir2.'/'.$dir3;
}

function get_avatar($uid, $size = 'middle', $type = '') {
	$size = in_array($size, array('big', 'middle', 'small')) ? $size : 'middle';
	$uid = abs(intval($uid));
	$uid = sprintf("%09d", $uid);
	$dir1 = substr($uid, 0, 3);
	$dir2 = substr($uid, 3, 2);
	$dir3 = substr($uid, 5, 2);
	$typeadd = $type == 'real' ? '_real' : '';
	return $dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).$typeadd."_avatar_$size.jpg";
}

?>
